<html>
<head>
    <meta charset="utf-8"/>
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Graphics绘制普通验证码 | 絷缘的博客</title>

<link rel="shortcut icon" href="https://zydnc.gitee.io/favicon.ico?v=1732378515642">

<link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://zydnc.gitee.io/styles/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css">

<script src="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dockerfile.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dart.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"></script>
<!-- DEMO JS -->
<!--<script src="media/scripts/index.js"></script>-->



    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.css">
 <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
</head>
<body>
<div class="main gt-bg-theme-color-first">
    <nav class="navbar navbar-expand-lg">
    <div class="navbar-brand">
        <img class="user-avatar" src="/images/avatar.png" alt="头像">
        <div class="site-name gt-c-content-color-first">
            絷缘的博客
        </div>
    </div>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <i class="fas fa-bars gt-c-content-color-first" style="font-size: 18px"></i>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <div class="navbar-nav mr-auto" style="text-align: center">
            
                <div class="nav-item">
                    
                        <a href="/" class="menu gt-a-link">
                            首页
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/archives" class="menu gt-a-link">
                            归档
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/tags" class="menu gt-a-link">
                            标签
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/about" class="menu gt-a-link">
                            关于
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/friends" class="menu gt-a-link">
                            友链
                        </a>
                    
                </div>
            
        </div>
        <div style="text-align: center">
            <form id="gridea-search-form" style="position: relative" data-update="1732378515642" action="/search/index.html">
                <input class="search-input" autocomplete="off" spellcheck="false" name="q" placeholder="搜索文章" />
                <i class="fas fa-search gt-c-content-color-first" style="position: absolute; top: 9px; left: 10px;"></i>
            </form>
        </div>
    </div>
</nav>

    <div class="post-container">
        <div class="post-detail gt-bg-theme-color-second">
            <article class="gt-post-content">
                <h2 class="post-title">
                    Graphics绘制普通验证码
                </h2>
                <div class="post-info">
                    <time class="post-time gt-c-content-color-first">
                        · 2021-01-06 ·
                    </time>
                    
                        <a href="https://zydnc.gitee.io/7FCmy-M7P/" class="post-tags">
                            # Graphics
                        </a>
                    
                        <a href="https://zydnc.gitee.io/java/" class="post-tags">
                            # Java
                        </a>
                    
                </div>
                <div class="post-content">
                    <h2 id="一-创建servletdemo继承httpservlet实现doget和dopost方法">一、创建ServletDemo继承HttpServlet，实现doGet和doPost方法</h2>
<pre><code class="language-java">@WebServlet(urlPatterns = &quot;/checkCodeServlet&quot;)
public class CheckServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int width = 100;
        int height = 30;
        String checkCodes = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;;
        int codeLen = 4;
        int lineCount = 20;
        int pointCount = 100;
        int spacing = width/codeLen;
        Random ran = new Random();
        StringBuilder sb = new StringBuilder();
        //创建画布
        BufferedImage bufferedImage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        //创建画笔
        Graphics graphics = bufferedImage.createGraphics();
        //设置画笔颜色
        graphics.setColor(new Color(238,238,238));
        //填充背景
        graphics.fillRect(0,0,width,height);
        //设置字体
        Font font  = new Font(&quot;微软雅黑&quot;,Font.BOLD,20);
        if(font!=null){
            graphics.setFont(font);
        }
        //重设颜色
        graphics.setColor(new Color(162,162,162));
        //绘制干扰点
        for (int i = 0; i &lt; pointCount; i++) {
            int x = MyUtils.getRanNum(0,width);
            int y = MyUtils.getRanNum(0,height);
            graphics.drawLine(x,y,x+1,y+1);
        }
        //重设颜色
        graphics.setColor(new Color(38,190,133));
        //绘制验证码
        for (int i = 0; i &lt; codeLen; i++) {
            String code = String.valueOf(checkCodes.charAt(MyUtils.getRanNum(0,checkCodes.length())));
            int x = MyUtils.getRanNum(i*spacing,Math.abs((i+1)*spacing-font.getSize()/2));
            int y = MyUtils.getRanNum(font.getSize(),height);
            graphics.drawString(code, x,y);
            sb.append(code);
            System.out.println(&quot;x:&quot;+x+&quot;\ty:&quot;+y);
        }
        //重设颜色
        graphics.setColor(new Color(148,218,248));
        //绘制干扰线
        for (int i = 0; i &lt; lineCount; i++) {
            int x1 = MyUtils.getRanNum(0,width);
            int y1 = MyUtils.getRanNum(0,height);
            int x2 = MyUtils.getRanNum(x1,width);
            int y2 = MyUtils.getRanNum(y1,height);
            graphics.drawLine(x1,y1,x2,y2);
        }
        System.out.println(sb);
        request.getSession().setAttribute(&quot;checkCodeSession&quot;,sb.toString());
        ImageIO.write(bufferedImage,&quot;png&quot;,response.getOutputStream());
    }
}
</code></pre>
<h2 id="二-实际效果如下">二、实际效果如下</h2>
<figure data-type="image" tabindex="1"><img src="https://zydnc.gitee.io/post-images/1610245282251.png" alt="" loading="lazy"></figure>
<blockquote>
<p>这种验证码还是比较古老了，早已经不再安全了，写这篇文章就是为了记录一下验证码的简单绘制，不适合用于项目中的验证</p>
</blockquote>
<blockquote>
<p>原文作者：絷缘<br>
作者邮箱：zhiyuanworkemail@163.com<br>
原文地址：<a href="https://zhiyuandnc.github.io/zEKFZIKMH/">https://zhiyuandnc.github.io/zEKFZIKMH/</a><br>
版权声明：本文为博主原创文章，转载请注明原文链接作者信息</p>
</blockquote>

                </div>
            </article>
        </div>

        
            <div class="next-post">
                <div class="next gt-c-content-color-first">下一篇</div>
                <a href="https://zydnc.gitee.io/UBe69VI0p/" class="post-title gt-a-link">
                    正则表达式模拟实现C3P0读取配置文件创建Connection对象
                </a>
            </div>
        

        

        

        

        <div class="site-footer gt-c-content-color-first">
    <div class="slogan gt-c-content-color-first">事实不以人的意志为转移</div>
    <div class="social-container">
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-github gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-telegram gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-qq gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-bilibili gt-c-content-color-first"></i>
                </a>
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
    </div>
    <div class="footer-info">
        Copyright&ensp;&copy;&ensp;絷缘的博客<br/><a href="https://beian.miit.gov.cn/" target="_blank">晋ICP备20001723号-1</a>
    </div>
    <div>
        Theme by <a href="https://imhanjie.com/" target="_blank">imhanjie</a>, Powered by <a
                href="https://github.com/getgridea/gridea" target="_blank">Gridea | <a href="https://zydnc.gitee.io/atom.xml" target="_blank">RSS</a></a>
    </div>
</div>

<script>
  hljs.initHighlightingOnLoad()
</script>

    </div>
</div>
</body>
</html>
